package com.singzi.leetcode.jianzhiOffer;

import com.singzi.leetcode.obj.TreeNode;
import sun.management.snmp.jvmmib.EnumJvmClassesVerboseLevel;

import java.util.LinkedList;
import java.util.Queue;

public class T0027 {
}
class Solution0027 {
    public TreeNode mirrorTree(TreeNode root) {
        if(root==null) return root;
        Queue<TreeNode> queueTree = new LinkedList<>();

        queueTree.add(root);
        while (!queueTree.isEmpty()){
            TreeNode treeNode = queueTree.remove();
            if(treeNode.left == null && treeNode.right == null) continue;
            if(treeNode.left != null && treeNode.right != null){
                TreeNode treeNodeLeft = new TreeNode();
                treeNodeLeft = treeNode.right;
                treeNode.right = treeNode.left;
                treeNode.left = treeNodeLeft;
                queueTree.add(treeNode.left);
                queueTree.add(treeNode.right);
            }
            else if(treeNode.left!=null){
                treeNode.right = treeNode.left;
                treeNode.left = null;
                queueTree.add(treeNode.right);
            }
            else {
                treeNode.left = treeNode.right;
                treeNode.right = null;
                queueTree.add(treeNode.left);
            }

        }
        return root;

    }
}